package cn.edu.thu.laud.jdbc;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import cn.edu.thu.laud.jdbc.hivecassandra.HideCassandraDataSource;
import cn.edu.thu.laud.jdbc.hivecassandra.HideHiveDataSource;
/**
 * Laud dataSource. 
 * this dataSource contain hiveDataSource and cassandraDataSource.
 * But Cassandra plays a primary role. 
 * @author hxd
 *
 */
public class LaUDDataSource implements DataSource {
	HideCassandraDataSource cassandraDataSource;
	HideHiveDataSource hiveDataSource;
	public LaUDDataSource(String host, int port, String keyspace, String user, String password)
    {
        cassandraDataSource=new HideCassandraDataSource(host, port, keyspace, user, password);
        hiveDataSource=new HideHiveDataSource();
    }
	
	@Override
	public Connection getConnection() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Connection getConnection(String username, String password)
			throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public PrintWriter getLogWriter() throws SQLException {
		return cassandraDataSource.getLogWriter();
	}

	@Override
	public int getLoginTimeout() throws SQLException {
		return cassandraDataSource.getLoginTimeout();
	}

	@Override
	public void setLogWriter(PrintWriter out) throws SQLException {
		 cassandraDataSource.setLogWriter(out);

	}

	@Override
	public void setLoginTimeout(int seconds) throws SQLException {
		cassandraDataSource.setLoginTimeout(seconds);
	}

	/**
	 * check if this dataSource can be convert to a CassandraDataSource or HiveDataSource.
	 * <br>
	 * @param iface can be a HideHiveDataSource Class or CassandraDataSource 
	 */
	@Override
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		return (iface.isAssignableFrom(HideCassandraDataSource.class)||iface.isAssignableFrom(HideHiveDataSource.class));
	}
	/**
	 * by this method ,this dataSource can be convert to a CassandraDataSource or HiveDataSource.
	 * <br>
	 * @param iface can be a HideHiveDataSource Class or CassandraDataSource 
	 */
	@Override
	public <T> T unwrap(Class<T> iface) throws SQLException {
		if(iface.isAssignableFrom(HideCassandraDataSource.class)){
			return iface.cast(HideCassandraDataSource.class);
		}else if(iface.isAssignableFrom(HideHiveDataSource.class)){
			return iface.cast(HideHiveDataSource.class);
		}else{
			return cassandraDataSource.unwrap(iface);
		}
			
	}



}
